/**
 * Created by Administrator on 2018/12/12.
 */
import Vue from 'vue';
import axios from 'axios';
import ElementUI from 'element-ui';
Vue.use(ElementUI);
import router from '../router';
import {strToHexCharCode,toFixedBit,dec2hex,formatTime,getNewDate,formatDateTime } from  './index.js';
// import MD5 from 'blueimp-md5';
var mdSecret ='fangyou968123456';
Vue.prototype.switch=false;
axios.interceptors.request.use(function (config) {
    var currentTime =parseInt(getNewDate((formatTime(new Date()))).getTime()/1000);
    config.headers={
        rand:sessionStorage.getItem('rand')||''
        // token:toFixedBit(strToHexCharCode(`${mdSecret}${currentTime}`),52,true,0,true),
        // timeStamp:parseInt(getNewDate((formatTime(new Date()))).getTime()/1000),
        // sign:MD5(`${sessionStorage.getItem('user_name')}${sessionStorage.getItem('token')}${currentTime}`)
    }
    // // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});
axios.interceptors.response.use(data => {// 响应成功关闭loading
    if(Vue.prototype.switch){
        return data;
    }else{
        if(data.data.code=="250001"||data.data.code=="100005"||data.data.code=="250002"){
            Vue.prototype.switch =true;
            Vue.prototype.$alert(data.data.message,'提示',{
                confirmButtonText: '确定',
                callback: action => {
                    router.push({
                        path:'/'
                    });
                    return false;
                }
            });
        }else if(data.data.code=="230008"){
            router.push({
                path:'/401'
            })
        }
    }
    return data
},error => {
    return Promise.reject(error)
});

export  default axios;
